/*
 * @lc app=leetcode.cn id=28 lang=javascript
 *
 * [28] 实现 strStr()
 */

// @lc code=start
/**
 * @param {string} haystack
 * @param {string} needle
 * @return {number}
 */
var strStr = function(haystack, needle) {
    let strIndex = 0
    let findFlag = false
    for (let index = 0; index < haystack.length + 1; index++) {
        if (needle[strIndex] === undefined) {
            findFlag = true
            return index - strIndex
        } 
        if (haystack[index] === needle[strIndex]) {
            ++strIndex
        } else {
            index -= strIndex
            strIndex = 0
        }
    }
    return findFlag ? strIndex : -1
};
// @lc code=end

/* 
    双指针求解
*/

